from PIL import Image,ImageColor,ImageDraw,ImageFont

from random import randint
from DOM.mysqlDB import MysqlDB
from string import digits,ascii_letters
import time
import hashlib



#激活码的生成

class ReCodeKey(object):

    def activationCode(self,num):
        length = num
        lists = []
        for i in range(length):
            x = randint(1, 2)
            if x == 1:
                y1 = randint(0, 9)
                lists.append(self.codeToolnum(y1))
            elif x == 2:
                y2 = randint(0, 47)
                lists.append(self.codeToolstr(y2))
        liststr = "".join(lists)
        ti = str(time.time())
        liststr = liststr + ti
        return liststr

    # 数字
    def codeToolnum(self,n):
        self.n=n
        x = digits[self.n]
        return x

    # 大小写字母
    def codeToolstr(self,m):
        self.m=m
        x = ascii_letters[self.m]
        return x

    # 加密hashlib
    def encrypt(self,str_code):
        m = hashlib.md5(str_code.encode(encoding='utf-8'))
        return m.hexdigest()

    def runcreate(self):
        str = self.activationCode(10)
        keys = self.encrypt(str)
        result=MysqlDB.RegisterCreate(keys)
        if result == True:
            print("CodeKey insert succsee")

        else:
            print("CodeKey insert false")

# for i in range(20):
#     CDK = ReCodeKey()
#     CDK.runcreate()
